package org.openotdr.server.file.xml;

import java.io.StringWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;


public abstract class DataParser {
	
	abstract protected Integer getColumnCount();
	abstract protected Integer getRowCount();
	abstract protected Integer getTitleRow();
	abstract protected String getUnit(Integer col);
	abstract protected String getValue(Integer row, Integer col);
	abstract protected String getName();
	
	ChartParser chartParser = null;
	TableParser tableParser = null;
	
	public void setChartParser(ChartParser chartParser) {
		this.chartParser = chartParser;
	}
	
	public void setTableParser(TableParser tableParser) {
		this.tableParser = tableParser;
	}
	
	public String getXML() {

		String ret = "";
		try {
			DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder docbuilder = dbfactory.newDocumentBuilder(); // 
			Document document = docbuilder.newDocument();          //

			Element root = document.createElement("NixyData");    // 
			document.appendChild(root);                
  			
			// Name
			Element name = document.createElement("Name");
			root.appendChild(name);
			Text nameVlaue= document.createTextNode(getName());
			name.appendChild(nameVlaue);
			
			// Column Count
			Element columns = document.createElement("Columns");
			root.appendChild(columns);
			Text colText= document.createTextNode(getColumnCount().toString());
			columns.appendChild(colText);

			// Row Count
			Element rows = document.createElement("Rows");
			root.appendChild(rows);
			Text rowText = document.createTextNode(getRowCount().toString());
			rows.appendChild(rowText);

			// Title Row
			Element titleRow = document.createElement("TitleRow");
			root.appendChild(titleRow);
			Text rowTitleText = document.createTextNode(getTitleRow().toString());
			titleRow.appendChild(rowTitleText);
			
			for (Integer col = 0; col < getColumnCount(); col++) {
				Element colElement = document.createElement("Data");
				root.appendChild(colElement);
				colElement.setAttribute("Column", col.toString());
				colElement.setAttribute("Unit", getUnit(col));
				for (Integer row = 0; row < getRowCount(); row++) {
					Element rowElement = document.createElement("D");
					colElement.appendChild(rowElement);
					Text rowVal = document.createTextNode(getValue(row, col));
					rowElement.appendChild(rowVal);
				}
			}
			

			if (chartParser != null) {
				chartParser.parse(document, root);
			}
			
			if (tableParser != null) {
				tableParser.parse(document, root);
			}
			
			System.setProperty("javax.xml.transform.TransformerFactory",
				"com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl");
			TransformerFactory tfactory = TransformerFactory.newInstance(); 
			Transformer transformer = tfactory.newTransformer(); 
			StringWriter writer = new StringWriter();
			transformer.transform(new DOMSource(document), new StreamResult(writer));
			ret = writer.toString();

        } catch (ParserConfigurationException e) {
          e.printStackTrace();
        } catch (TransformerException e) {
          e.printStackTrace();
        }
        
        return ret;
	}

}
